package net.ufrog.common;

import net.ufrog.common.app.App;
import net.ufrog.common.exception.ServiceException;
import net.ufrog.common.utils.Strings;

import java.nio.charset.Charset;
import java.util.regex.Pattern;

/**
 * @author ultrafrog, ufrog.net@gmail.com
 * @version 0.1, 2017-02-03
 * @since 0.1
 */
public class CoreConfig {

    public static final String FILE_CONF                    = "/application.conf";
    public static final String FILE_YAML                    = "/application.yaml";
    public static final String FILE_MIMETYPES               = "mimetypes.properties";

    private static final String KEY_SECRET                  = "app.secret";
    private static final String KEY_CHARSET                 = "app.charset";
    private static final String KEY_PATTERN_DATE            = "app.format.data";
    private static final String KEY_PATTERN_TIME            = "app.format.time";
    private static final String KEY_PATTERN_DATETIME        = "app.format.datetime";
    private static final String KEY_PATTERN_CURRENCY        = "app.format.currency";
    private static final String KEY_PATTERN_PERCENT         = "app.format.percent";
    private static final String KEY_LOGGER_NAME             = "app.logger.name";
    private static final String KEY_LOGGER_CALLER           = "app.logger.caller";
    private static final String KEY_MESSAGES                = "app.messages";
    private static final String KEY_PATTERN_UUID            = "pattern.uuid";
    private static final String KEY_PATTERN_CELLPHONE       = "pattern.cellphone";
    private static final String KEY_PATTERN_EMAIL           = "pattern.email";
    private static final String KEY_MAIL_SMTP_HOST          = "mail.smtp.host";
    private static final String KEY_MAIL_SMTP_PORT          = "mail.smtp.port";
    private static final String KEY_MAIL_SMTP_USERNAME      = "mail.smtp.username";
    private static final String KEY_MAIL_SMTP_PASSWORD      = "mail.smtp.password";
    private static final String KEY_MAIL_FROM               = "mail.from";
    private static final String KEY_MAIL_FROM_NAME          = "mail.from.name";
    private static final String KEY_QR_MARGIN               = "qr.margin";

    private static final String DEFAULT_CHARSET             = "utf-8";
    private static final String DEFAULT_PATTERN_DATE        = "yyyy-MM-dd";
    private static final String DEFAULT_PATTERN_TIME        = "HH:mm:ss";
    private static final String DEFAULT_PATTERN_DATETIME    = "yyyy-MM-dd HH:mm:ss";
    private static final String DEFAULT_PATTERN_CURRENCY    = "#,##0.00";
    private static final String DEFAULT_PATTERN_PERCENT     = "#,##0.00%";
    private static final String DEFAULT_LOGGER_NAME         = "app";
    private static final String DEFAULT_LOGGER_CALLER       = "false";
    private static final String DEFAULT_QR_MARGIN           = "1";

    private static String secret;
    private static Charset charset;
    private static String patternDate;
    private static String patternTime;
    private static String patternDatetime;
    private static String patternCurrency;
    private static String patternPercent;
    private static String loggerName;
    private static Boolean loggerCaller;

    /**
     * @return application config for secret
     */
    public static String getSecret() {
        if (Strings.empty(secret)) {
            secret = App.config(KEY_SECRET);
            if (Strings.empty(secret)) {
                throw new ServiceException("app secret not defined.");
            }
        }
        return secret;
    }

    /**
     * @return application config for charset
     */
    public static Charset getCharset() {
        if (charset == null) {
            charset = Charset.forName(App.config(KEY_CHARSET, DEFAULT_CHARSET));
        }
        return charset;
    }

    /**
     * @return application config for pattern date
     */
    public static String getPatternDate() {
        if (Strings.empty(patternDate)) {
            patternDate = App.config(KEY_PATTERN_DATE, DEFAULT_PATTERN_DATE);
        }
        return patternDate;
    }

    /**
     * @return application config for pattern time
     */
    public static String getPatternTime() {
        if (Strings.empty(patternTime)) {
            patternTime = App.config(KEY_PATTERN_TIME, DEFAULT_PATTERN_TIME);
        }
        return patternTime;
    }

    /**
     * @return application config for pattern datetime
     */
    public static String getPatternDatetime() {
        if (Strings.empty(patternDatetime)) {
            patternDatetime = App.config(KEY_PATTERN_DATETIME, DEFAULT_PATTERN_DATETIME);
        }
        return patternDatetime;
    }

    /**
     * @return application config for pattern currency
     */
    public static String getPatternCurrency() {
        if (Strings.empty(patternCurrency)) {
            patternCurrency = App.config(KEY_PATTERN_CURRENCY, DEFAULT_PATTERN_CURRENCY);
        }
        return patternCurrency;
    }

    /**
     * @return application config for pattern percent
     */
    public static String getPatternPercent() {
        if (Strings.empty(patternPercent)) {
            patternPercent = App.config(KEY_PATTERN_PERCENT, DEFAULT_PATTERN_PERCENT);
        }
        return patternPercent;
    }

    /**
     * @return application config for logger name
     */
    public static String getLoggerName() {
        if (Strings.empty(loggerName)) {
            loggerName = App.config(KEY_LOGGER_NAME, DEFAULT_LOGGER_NAME);
        }
        return loggerName;
    }

    /**
     * @return application config for logger caller
     */
    public static Boolean getLoggerCaller() {
        if (loggerCaller == null) {
            loggerCaller = Boolean.parseBoolean(App.config(KEY_LOGGER_CALLER, DEFAULT_LOGGER_CALLER));
        }
        return loggerCaller;
    }

    /**
     * @return application messages
     */
    public static String getMessages() {
        return App.config(KEY_MESSAGES);
    }

    /**
     * @return application config for pattern uuid
     */
    public static Pattern getPatternUUID() {
        return Pattern.compile(App.config(KEY_PATTERN_UUID));
    }

    /**
     * @return application config for pattern cellphone
     */
    public static Pattern getPatternCellphone() {
        return Pattern.compile(App.config(KEY_PATTERN_CELLPHONE));
    }

    /**
     * @return application config for pattern email
     */
    public static Pattern getPatternEmail() {
        return Pattern.compile(App.config(KEY_PATTERN_EMAIL));
    }

    /**
     * @return application config for qr margin
     */
    public static Integer getQrMargin() {
        return Integer.parseInt(App.config(KEY_QR_MARGIN, DEFAULT_QR_MARGIN));
    }

    /**
     * @return application config for mail smtp host
     */
    public static String getMailSmtpHost() {
        return App.config(KEY_MAIL_SMTP_HOST);
    }

    /**
     * @return application config for mail smtp port
     */
    public static String getMailSmtpPort() {
        return App.config(KEY_MAIL_SMTP_PORT);
    }

    /**
     * @return application config for mail smtp username
     */
    public static String getMailSmtpUsername() {
        return App.config(KEY_MAIL_SMTP_USERNAME);
    }

    /**
     * @return application config for mail smtp password
     */
    public static String getMailSmtpPassword() {
        return App.config(KEY_MAIL_SMTP_PASSWORD);
    }

    /**
     * @return application config for mail from
     */
    public static String getMailFrom() {
        return App.config(KEY_MAIL_FROM);
    }

    /**
     * @return application config for mail from name
     */
    public static String getMailFromName() {
        return App.config(KEY_MAIL_FROM_NAME);
    }
}
